package xw.qxypt.modules.baseinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import xw.qxypt.common.utils.BaseTree;
import xw.qxypt.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xw.qxypt.modules.baseinfo.entity.LhpDataEquipmentinfoEntity;
import xw.qxypt.modules.baseinfo.entity.LhpInstallationPointEntity;
import xw.qxypt.modules.baseinfo.service.LhpInstallationPointService;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.R;
import xw.qxypt.modules.socket.util.HspUtil;
import xw.qxypt.modules.sys.shiro.ShiroUtils;


/**
 * 安装点管理
 *
 * @author xw
 * @email 
 * @date 2019-11-28 16:38:08
 */
@RestController
@RequestMapping("baseinfo/lhpinstallationpoint")
public class LhpInstallationPointController {
    @Autowired
    private LhpInstallationPointService lhpInstallationPointService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("baseinfo:lhpinstallationpoint:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = lhpInstallationPointService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        LhpInstallationPointEntity lhpInstallationPoint = lhpInstallationPointService.getById(id);

        return R.ok().put("lhpInstallationPoint", lhpInstallationPoint);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("baseinfo:lhpinstallationpoint:save")
    public R save(@RequestBody LhpInstallationPointEntity lhpInstallationPoint){
        lhpInstallationPointService.save(lhpInstallationPoint);
        //初始化设备连接列表
        lhpInstallationPointService.refreshDataCollectionMap(HspUtil.map);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("baseinfo:lhpinstallationpoint:update")
    public R update(@RequestBody LhpInstallationPointEntity lhpInstallationPoint){
        ValidatorUtils.validateEntity(lhpInstallationPoint);
        lhpInstallationPointService.updateById(lhpInstallationPoint);
        //初始化设备连接列表
        lhpInstallationPointService.refreshDataCollectionMap(HspUtil.map);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("baseinfo:lhpinstallationpoint:delete")
    public R delete(@RequestBody String[] ids){
        lhpInstallationPointService.removeByIds(Arrays.asList(ids));
        //初始化设备连接列表
        lhpInstallationPointService.refreshDataCollectionMap(HspUtil.map);
        return R.ok();
    }

    /**
     * 路段--安装点树
     */
    @RequestMapping("/pointTree")
    public List<BaseTree> pointTree(){

        return lhpInstallationPointService.pointTree();
    }

    /**
     * 根据登录用户查询关联安装点
     */
    @RequestMapping("/getPointByUserId")
    public R getPointByUserId(){
        List<LhpInstallationPointEntity> list=lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        return R.ok().put("list",list);
    }
    /**
     * 根据登录用户查询自建站数据
     */
    @RequestMapping("/getDataByUserId")
    public R getDataByUserId(){
        Map<String, LhpDataEquipmentinfoEntity> dataMap=lhpInstallationPointService.getDataByUserId(ShiroUtils.getUserId());
        return R.ok().put("list",dataMap.values());
    }
}
